package com.icangya.DAO.impl;

import com.icangya.DAO.ICashRegisterDAO;
import com.icangya.Utils.SQLUtilt;
import com.icangya.domain.CashRegister;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import static com.icangya.Value.StaticValue.PHONE_PAGE_ROW;

@Repository
public class CashRegisterDAOImpl extends BaseDAO<CashRegister> implements ICashRegisterDAO {


    @Override
    public List getAllCashRecord() {

        String hql="FROM CashRegister";
       List<CashRegister> list= (List<CashRegister>) getHibernateTemplate().find(hql);
       if(list.size()>0)
           return list;
        return null;
    }

    @Override
    public List getCashRecordByOpenId(String openId) {

        String hql="FROM CashRegister cr where cr.openId=?  order by cr.createTime desc";
        List<CashRegister> list= (List<CashRegister>) getHibernateTemplate().find(hql,openId);
        if(list.size()>0)
            return list;
        return null;
    }

    @Override
    public List getCashRecordByOpenId(String openId, int startPage) {
        String hql="FROM CashRegister cr where cr.openId='"+openId+"'  order by cr.createTime desc";
        String count="select count(cr) "+hql;
        Long total = (Long) getHibernateTemplate().find(count).get(0);
        return SQLUtilt.getIstance().doPage(getHibernateTemplate(), hql, total, startPage, PHONE_PAGE_ROW);

//        List<CashRegister> list= (List<CashRegister>) getHibernateTemplate().find(hql,openId);
//        if(list.size()>0)
//            return list;
//        return null;

    }

    @Override
    public Double getCashMoneyNow(Timestamp time) {
        String hql="select sum(cr.money) from CashRegister cr where TO_DAYS(cr.createTime)=TO_DAYS(?)  and cr.status=0";
        Double money= (Double) getHibernateTemplate().find(hql,time).get(0);
        if(money==null)
            money=0.0;
        return money;
    }

    @Override
    public long getCountCashRecord(String openId) {
//        String hql="Select count(cr)  from CashRegister cr where  TO_DAYS(cr.createTime) =TO_DAYS(?) and cr.openId=? and cr.status=0";
        String hql="Select count(cr)  from CashRegister cr where  TO_DAYS(cr.createTime) =TO_DAYS(?) and cr.openId=? ";
      long count= (Long) getHibernateTemplate().find(hql,new Timestamp(new Date().getTime()),openId).get(0);
        return count;
    }

//      获取用户当天的提现总金额
    @Override
    public Double getSumCashMoney(String openId) {
        String hql="select sum(cr.money) from CashRegister cr where TO_DAYS(cr.createTime)=TO_DAYS(?)  and cr.status=0 and cr.openId=?";

      Double sumMoney= (Double) getHibernateTemplate().find(hql,new Timestamp(new Date().getTime()),openId).get(0);
      if(sumMoney==null)
          return 0.0;
        return sumMoney;
    }
}






















